﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using ResourceCollectionSys.Service;
using SpiderCore.Service;
using Timer = System.Windows.Forms.Timer;

namespace ResourceCollectionSys
{
    public partial class Form1 : Form
    {
        private static int ReviveTimes { get; set; }//复活次数
        private static int ReviveIntervalTime { get; set; }//复活间隔时间，小时
        private Timer timerforrefresh;
        private Timer timerforTask;
        private static string LastVerifyCode;
        public Form1()
        {
            InitializeComponent();
            nupSpriderWorkSpeed.DecimalPlaces = 2;
            nupSpriderWorkSpeed.Increment = 0.25M;
            timerforrefresh = new Timer();
            timerforrefresh.Interval = 500;
            timerforrefresh.Tick += new EventHandler(timerforrefresh_Tick);
            timerforrefresh.Start();
            timerforTask = new Timer();
            timerforTask.Interval = 2000;
            timerforTask.Tick += new EventHandler(timerforTask_Tick);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ThreadPool.SetMaxThreads((int)nudThreadpool.Value, (int)nudThreadpool.Value);
            SpiderService.SetSpriderCaptainWorkSpeed((int)(nupSpriderWorkSpeed.Value * 1000));
            ReviveTimes = (int)nudSystemReveTimes.Value;
            ReviveIntervalTime = (int)nudSystemReveInterval.Value;
            SpiderService.SetSpriderWebBrowserDelay((int)nudWebrowserDelay.Value);


        }
        private void timerforrefresh_Tick(object sender, EventArgs e)
        {
            lblSpriderRemainTask.Text = SpiderMonitor.GetTodoTaskCount().ToString();
            lblAntRemainTask.Text = SpiderMonitor.GetPageRepositoryCount().ToString();
            lblAntFinishedCount.Text = SpiderMonitor.GetVisitedCount().ToString();
            lblSpriderWorking.Text = SpiderMonitor.SpiderIsWorking ? "是" : "否";
            lblAntWorking.Text = SpiderMonitor.AntIsWorking ? "是" : "否";
            btnSpriderStartToWork.Text = SpiderMonitor.SpiderIsWorking ? "停止工作" : "开始工作";
            btnAntStartToWork.Text = SpiderMonitor.AntIsWorking ? "停止工作" : "开始工作";
            lblSpriderCaptainreason.Text = SpiderMonitor.SpiderCaptainStopWorkReason;
            lblAntCaptainReason.Text = SpiderMonitor.AntCaptainStopWorkReason;
            lblAntSuccessFinishedCount.Text = SpiderMonitor.AntSuccessFinishedTaskCount.ToString();
            lblAntFinishedCount.Text = SpiderMonitor.AntFinishedTaskCount.ToString();
            lblSpiderCaptainStartTime.Text = SpiderMonitor.SpiderCaptainStartWorkTime.ToString();
            lblSpiderCaptainEndTime.Text = SpiderMonitor.SpiderCaptainEndWorkTime.ToString();
            lblRemainReviewTimes.Text = ReviveTimes.ToString();
            if (SpiderMonitor.SpiderIsWorking && SpiderMonitor.AntIsWorking)
            {
                btnStart.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            }
            else
            {
                btnStart.BackColor = System.Drawing.Color.Red;
            }
            if (SpiderMonitor.AntFinishedTaskCount != 0)
            {
                lblAntSuccesRate.Text =
              (Math.Round(((double)SpiderMonitor.AntSuccessFinishedTaskCount / (double)SpiderMonitor.AntFinishedTaskCount), 2) * 100) + "%";
            }
            //数据转存
            btnTranslationVideo.Text = VideoTranslationService.IsWorcking ? "工作中" : "启动";
            btnTransalatBook.Text = BookTranslationService.IsWorcking ? "工作中" : "启动";
            btnBaiduPush.Text = BaiDuService.IsWorcking ? "工作中" : "启动";
            lblVideoSuccessCount.Text = VideoTranslationService.SuccessCount.ToString();
            lblTranslationBookSuccess.Text = BookTranslationService.SuccessCount.ToString();
            lblBaiduPushSuccess.Text = BaiDuService.SuccessCount.ToString();
            lblTranslationVideoErrorInfo.Text = VideoTranslationService.ErrorMessage;
            lblTranslationBookError.Text = BookTranslationService.ErrorMessage;
            lblBaiduPushError.Text = BaiDuService.ErrorMessage;
        }
        private void timerforTask_Tick(object sender, EventArgs e)
        {

            //系统自动重启
            if (ReviveTimes > 0 && SpiderMonitor.SpiderCaptainEndWorkTime != DateTime.MinValue && DateTime.Now - SpiderMonitor.SpiderCaptainEndWorkTime > new TimeSpan(0, 0, ReviveIntervalTime, 0))
            {
                btnStart_Click(null, null);
                ReviveTimes--;
            }
        }
        private void nudThreadpool_ValueChanged(object sender, EventArgs e)
        {
            ThreadPool.SetMaxThreads((int)nudThreadpool.Value, (int)nudThreadpool.Value);
        }

        private void nupSpriderWorkSpeed_ValueChanged(object sender, EventArgs e)
        {
            SpiderService.SetSpriderCaptainWorkSpeed((int)(nupSpriderWorkSpeed.Value * 1000));
        }

        private void btnSpriderStartToWork_Click(object sender, EventArgs e)
        {
            if (btnSpriderStartToWork.Text == "开始工作")
                SpiderService.CommandSpiderToWork();
            else
            {
                SpiderService.CommandSpiderStopToWork();
            }

        }

        private void btnAntStartToWork_Click(object sender, EventArgs e)
        {
            if (btnAntStartToWork.Text == "开始工作")
            {
                AntService.CommandAntCaptainToWork();
                SpiderService.ReSetSpiderConfig();
            }
            else
            {
                AntService.CommandAntCaptainStopToWork();
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            List<string> sites=new List<string>();
            if(cbCatchBoKu.Checked)
                sites.Add("BoKu");
            if (cbCatchKanKanSite.Checked)
                sites.Add("KanKan");
            this.BeginInvoke((ThreadStart)delegate() { ResourceManageService.StartToCatchResource(sites); });
            timerforTask.Start();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void nudWebrowserDelay_ValueChanged(object sender, EventArgs e)
        {
            SpiderService.SetSpriderWebBrowserDelay((int)nudWebrowserDelay.Value);
        }
        private void nudSystemReveTimes_ValueChanged(object sender, EventArgs e)
        {
            ReviveTimes = (int)nudSystemReveTimes.Value;
        }

        private void nudSystemReveInterval_ValueChanged(object sender, EventArgs e)
        {
            ReviveIntervalTime = (int)nudSystemReveInterval.Value;
        }

        private void btnSaveTask_Click(object sender, EventArgs e)
        {
            int count = 0;
            if (ResourceManageService.SaveTask(out count))
                MessageBox.Show("保存成功!数量："+count);
            else
                MessageBox.Show("保存失败");
        }
         
        private void btnRecoverTask_Click(object sender, EventArgs e)
        {
            int count = 0;
            if (ResourceManageService.Recovertask(out count))
                MessageBox.Show("恢复成功!数量：" + count);
            else
                MessageBox.Show("恢复失败");
        }

        private void btnTranslationVideo_Click(object sender, EventArgs e)
        {
            if (btnTranslationVideo.Text == "启动")
            {
                Thread th=new Thread(VideoTranslationService.EditContent);
                th.Start();
            }
            
            else
            {
                VideoTranslationService.IsWorcking = false;
            }
        }

        private void cbCatchDangDang_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnTransalatBook_Click(object sender, EventArgs e)
        {
            if (btnTransalatBook.Text == "启动")
            {
                Thread th = new Thread(BookTranslationService.TanslationBookDataToBBs);
                th.Start();
            }

            else
            {
                BookTranslationService.IsWorcking = false;
            }
        }
        private void btnAddBoKuSeedSite_Click(object sender, EventArgs e)
        {
            ResourceManageService.AddSiteSeed(new string[] { txtBoKuSiteSeed.Text},"book", "BoKu" );
        }

        private void btnAddKanKanSiteSeed_Click(object sender, EventArgs e)
        {
            ResourceManageService.AddSiteSeed(new string[] { txtBoKuSiteSeed.Text }, "video", "KanKan");
        }

        private void btnBaiduPush_Click(object sender, EventArgs e)
        {
            if (btnTransalatBook.Text == "启动")
            {
                Thread th = new Thread(BaiDuService.PushUrlToBaiDu);
                th.Start();
            }

            else
            {
                BaiDuService.IsWorcking = false;
            }
        }

        private void btnSyncPushRecord_Click(object sender, EventArgs e)
        {
           var count=BaiDuService.SyscPushRecord();
           MessageBox.Show("同步成功！" + count);
        }

    }
}
